/****************** Copyright(c) HDM  ***********************
* 
*------File Info--------------------------------------------------------------
* File Name: 文件名
* Latest modified Date: 最终修改日期（YYYY-MM-DD）
* Latest Version:       最终修订版本号
* Description:          文件的简要描述信息
* compiler:             MDK v4.74
* MCU:                  STM32F103VE
* Oscillator Crystal Frequency:    8MHz
*-----------------------------------------------------------------------------
* Created By:    文件的创建者姓名
* Created date:  文件的创建日期（YYYY-MM-DD）
* Version:       文件创建时的版本号
* Descriptions:  文件创建时的简要描述
*
*-----------------------------------------------------------------------------
* Modified by:   修改人的姓名
* Modified date: 文件的修改日期（YYYY-MM-DD）
* Version:       文件修订的版本号
* Description:   文件修订的简要描述
*
******************************************************************************/


/* Includes -----------------------------------------------------------------*/
#include <stdint.h>                    /* data type definitions and macro*/
//#include "bsp.h"
#include "stm32f1xx_hal.h"
//#include "bsp_modbus.h"
#include "bsp_port.h"
/* Private typedef ----------------------------------------------------------*/
/* Private define -----------------------------------------------------------*/
/* Private macro ------------------------------------------------------------*/
/* Private constants ------------------------------------------------------- */
/* Private variables --------------------------------------------------------*/
/* Private function prototypes ----------------------------------------------*/

/* Private functions --------------------------------------------------------*/
static uint32_t OutputState;                                     /* 记录输出状态 */
/* Declaration of extern functions ------------------------------------------*/
/* Global variables ---------------------------------------------------------*/



/** @defgroup bsp_port_functions
  * @{
  */

/**
  * @brief  SetDO1 function 设置输出口DO1状态
  * @param  _state 0:关闭; 1:打开
  * @retval None
  */
 void SetDO1(uint8_t _state)
{
    if (_state == 0)
    {
        OutputState &= 0xfffffffe;
        HAL_GPIO_WritePin(DOUT1_GPIO, DOUT1_PIN, GPIO_PIN_SET);
    }
    else
    {
        OutputState |= 0x00000001;
        HAL_GPIO_WritePin(DOUT1_GPIO, DOUT1_PIN, GPIO_PIN_RESET);
    }
}

/**
  * @brief  SetDO2 function 设置输出口DO2状态
  * @param  _state 0:关闭; 1:打开
  * @retval None
  */
 void SetDO2(uint8_t _state)
{
    if (_state == 0)
    {
        OutputState &= 0xfffffffd;
        HAL_GPIO_WritePin(DOUT2_GPIO, DOUT2_PIN, GPIO_PIN_SET);
    }
    else
    {
        OutputState |= 0x00000002;
        HAL_GPIO_WritePin(DOUT2_GPIO, DOUT2_PIN, GPIO_PIN_RESET);
    }
}

/**
  * @brief  SetDO3 function 设置输出口DO3状态
  * @param  _state 0:关闭; 1:打开
  * @retval None
  */
 void SetDO3(uint8_t _state)
{
    if (_state == 0)
    {
        OutputState &= 0xfffffffb;
        HAL_GPIO_WritePin(DOUT3_GPIO, DOUT3_PIN, GPIO_PIN_SET);
    }
    else
    {
        OutputState |= 0x00000004;
        HAL_GPIO_WritePin(DOUT3_GPIO, DOUT3_PIN, GPIO_PIN_RESET);
    }
}

/**
  * @brief  SetDO4 function 设置输出口DO4状态
  * @param  _state 0:关闭; 1:打开
  * @retval None
  */
 void SetDO4(uint8_t _state)
{
    if (_state == 0)
    {
        OutputState &= 0xfffffff7;
        HAL_GPIO_WritePin(DOUT4_GPIO, DOUT4_PIN, GPIO_PIN_SET);
    }
    else
    {
        OutputState |= 0x00000008;
        HAL_GPIO_WritePin(DOUT4_GPIO, DOUT4_PIN, GPIO_PIN_RESET);
    }
}
/**
  * @brief  SetDO5 function 设置输出口DO5状态
  * @param  _state 0:关闭; 1:打开
  * @retval None
  */
 void SetDO5(uint8_t _state)
{
    if (_state == 0)
    {
        OutputState &= 0xffffffef;
        HAL_GPIO_WritePin(DOUT5_GPIO, DOUT5_PIN, GPIO_PIN_SET);
    }
    else
    {
        OutputState |= 0x00000010;
        HAL_GPIO_WritePin(DOUT5_GPIO, DOUT5_PIN, GPIO_PIN_RESET);
    }
}

/**
  * @brief  SetDO6 function 设置输出口DO6状态
  * @param  _state 0:关闭; 1:打开
  * @retval None
  */
void SetDO6(uint8_t _state)
{
    if (_state == 0)
    {
        OutputState &= 0xffffffdf;
        HAL_GPIO_WritePin(DOUT6_GPIO, DOUT6_PIN, GPIO_PIN_SET);
    }
    else
    {
        OutputState |= 0x00000020;
        HAL_GPIO_WritePin(DOUT6_GPIO, DOUT6_PIN, GPIO_PIN_RESET);
    }
}

/**
  * @brief  SetDO7 function 设置输出口DO7状态
  * @param  _state 0:关闭; 1:打开
  * @retval None
  */
 void SetDO7(uint8_t _state)
{
    if (_state == 0)
    {
        OutputState &= 0xffffffbf;
        HAL_GPIO_WritePin(DOUT7_GPIO, DOUT7_PIN, GPIO_PIN_SET);
    }
    else
    {
        OutputState |= 0x00000040;
        HAL_GPIO_WritePin(DOUT7_GPIO, DOUT7_PIN, GPIO_PIN_RESET);
    }
}

/**
  * @brief  SetDO8 function 设置输出口DO8状态
  * @param  _state 0:关闭; 1:打开
  * @retval None
  */
 void SetDO8(uint8_t _state)
{
    if (_state == 0)
    {
        OutputState &= 0xffffff7f;
        HAL_GPIO_WritePin(DOUT8_GPIO, DOUT8_PIN, GPIO_PIN_SET);
    }
    else
    {
        OutputState |= 0x00000080;
        HAL_GPIO_WritePin(DOUT8_GPIO, DOUT8_PIN, GPIO_PIN_RESET);
    }
}

/**
  * @brief  SetOutput function
  * @param _channel 输出通道
  *        _state   输出口的状态值
  * @retval none                
  */
void SetOutputBit(uint8_t _channel, uint8_t _state)
{
    switch (_channel)
    {
    case 0:
        SetDO1(_state);
        break;
    case 1:
        SetDO2(_state);
        break;
    case 2:
        SetDO3(_state);
        break;
    case 3:
        SetDO4(_state);
        break;
    case 4:
        SetDO5(_state);
        break;
    case 5:
        SetDO6(_state);
        break;
    case 6:
        SetDO7(_state);
        break;
    case 7:
        SetDO8(_state);
        break;
    default:
        break;
    }
}

/**
  * @brief  SetOutput function
  * @param _state   输出口的状态值 
  * @retval none                
  */
void SetOutput(uint32_t _state)
{
    uint8_t mask;
    uint8_t i;

    for (i = 0, mask = 1; i < 4; i++)
    {
        if (_state & mask)
        {
            SetOutputBit(i, ON);
        }
        else
        {
            SetOutputBit(i, OFF);
        }
        mask <<= 1;
    }
}

/**
  * @brief  Read output state
  * @param  none
  * @retval output port state                
  */
uint32_t ReadOutput(void)
{
    return OutputState;
}

/**
  * @brief  ReadInput function
  * @param  None
  * @retval 8位数据对应8个输入口状态,为了统一接口,统一32位
  */
uint32_t ReadInput(void)
{
    uint32_t state = 0;

    if (HAL_GPIO_ReadPin(DIN1_GPIO, DIN1_PIN) != GPIO_PIN_RESET)
    {
        state |= (uint8_t)0x01;
    }

    if (HAL_GPIO_ReadPin(DIN2_GPIO, DIN2_PIN) != GPIO_PIN_RESET)
    {
        state |= (uint8_t)0x02;
    }

    if (HAL_GPIO_ReadPin(DIN3_GPIO, DIN3_PIN) != GPIO_PIN_RESET)
    {
        state |= (uint8_t)0x04;
    }

    if (HAL_GPIO_ReadPin(DIN4_GPIO, DIN4_PIN) != GPIO_PIN_RESET)
    {
        state |= (uint8_t)0x08;
    }

    if (HAL_GPIO_ReadPin(DIN5_GPIO, DIN5_PIN) != GPIO_PIN_RESET)
    {
        state |= (uint8_t)0x10;
    }

    if (HAL_GPIO_ReadPin(DIN6_GPIO, DIN6_PIN) != GPIO_PIN_RESET)
    {
        state |= (uint8_t)0x20;
    }

    if (HAL_GPIO_ReadPin(DIN7_GPIO, DIN7_PIN) != GPIO_PIN_RESET)
    {
        state |= (uint8_t)0x40;
    }

    if (HAL_GPIO_ReadPin(DIN8_GPIO, DIN8_PIN) != GPIO_PIN_RESET)
    {
        state |= (uint8_t)0x80;
    }

    return state;
}

/**
  * @brief  ReadInput function
  * @param  _index DI端口序号 
  * @retval 指定DI端口的状态
  */
uint8_t ReadDI(uint16_t _index)
{
    uint8_t state = OFF;

    switch (_index)
    {
    case 0:
        if (HAL_GPIO_ReadPin(DIN1_GPIO, DIN1_PIN) != GPIO_PIN_RESET)
        {
            state = ON;
        }
        break;
    case 1:
        if (HAL_GPIO_ReadPin(DIN2_GPIO, DIN2_PIN) != GPIO_PIN_RESET)
        {
            state = ON;
        }
        break;
    case 2:
        if (HAL_GPIO_ReadPin(DIN3_GPIO, DIN3_PIN) != GPIO_PIN_RESET)
        {
            state = ON;
        }
        break;
    case 3:
        if (HAL_GPIO_ReadPin(DIN4_GPIO, DIN4_PIN) != GPIO_PIN_RESET)
        {
            state = ON;
        }
        break;
    case 4:
        if (HAL_GPIO_ReadPin(DIN5_GPIO, DIN5_PIN) != GPIO_PIN_RESET)
        {
            state = ON;
        }
        break;
    case 5:
        if (HAL_GPIO_ReadPin(DIN6_GPIO, DIN6_PIN) != GPIO_PIN_RESET)
        {
            state = ON;
        }
        break;
    case 6:
        if (HAL_GPIO_ReadPin(DIN7_GPIO, DIN7_PIN) != GPIO_PIN_RESET)
        {
            state = ON;
        }
        break;
    case 7:
        if (HAL_GPIO_ReadPin(DIN8_GPIO, DIN8_PIN) != GPIO_PIN_RESET)
        {
            state = ON;
        }
        break;
    default:
        break;
    }

    return state;
}






/************** (C) COPYRIGHT HDM  ********END OF FILE****/
